/**
 * 招聘渠道
 */
import React from 'react';
import { Select } from 'antd';
import { useRequest } from '@/hooks';
import { dictionaryList } from '@/services/dictionary';

const SelectDict = React.forwardRef(({ value, onChange, code, agreeFilter = false, ...rest }, ref) => {
    const { data = {}, run } = useRequest(() => dictionaryList({ codes: [code] }), { manual: true });
    const options = React.useMemo(() => (!agreeFilter ? data.data : data?.data?.filter(v => v.remark !== '1')) || [], [
        data
    ]);

    React.useEffect(() => {
        if (code) {
            run();
        }
    }, []);

    const finalValue = typeof value === 'number' ? String(value) : value;

    return (
        <Select
            ref={ref}
            value={finalValue}
            onChange={(_value, item = {}) => {
                const { props = {} } = item;
                const { children } = props;
                onChange(_value, children);
            }}
            placeholder="请选择"
            allowClear
            {...rest}
        >
            {options.map(item => (
                <Select.Option key={item.dictValue} value={item.dictValue}>
                    {item.dictName}
                </Select.Option>
            ))}
        </Select>
    );
});

export default SelectDict;
